package ferhrosa.android;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class ListViewAdapter extends ArrayAdapter<ListViewItem> {

    private Activity activity = null;
    private List<ListViewItem> items = null;
    private int templateId = 0;
    public static final int TPL_1 = 1;
    public static final int TPL_2 = 2;
    public static final int TPL_3 = 3;

    public int getTemplateId() {
        return templateId;
    }

    public Activity getActivity() {
        return activity;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public List<ListViewItem> getItems() {
        return items;
    }

    public void setItems(List<ListViewItem> items) {
        this.items = items;
    }

    public ListViewAdapter(Activity activity, List<ListViewItem> items,
            int templateId) {
        super(activity, android.R.layout.simple_list_item_1, items);
        this.items = items;
        this.activity = activity;
        this.templateId = templateId;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ListViewWrapper wrapper = null;
        if (row == null) {
            LayoutInflater inflater = activity.getLayoutInflater();
            row = inflater.inflate(R.layout.listview, null);
            wrapper = new ListViewWrapper(row);
            row.setTag(wrapper);

        } else {
            wrapper = (ListViewWrapper) row.getTag();
        }

        wrapper.setTemplateId(templateId);
        wrapper.populateFrom(items.get(position));
        return (row);
    }

}